package com.wc._16届国特冲刺营._05动态规划_状态压缩黑科技.扫雷2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/5/9 14:52
 * @description
 * https://www.lanqiao.cn/courses/51805/learning/?id=4072907&compatibility=false
 */
public class Main {
    /**
     * 思路：
     * 状压 DP
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 500010, M = 8, P = 998244353;
    // f[i][j] 表示 第 i 行 上面状态为 j, 下面状态为 k 的合法方案数
    static long[][][] f = new long[2][M][M];
    static int[] cnt = new int[M];
    static int[] w = new int[N];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) w[i] = sc.nextInt();

        // n = 1 样例中不存在
        if (n == 1) {
            long res = 0;
            if (w[1] <= 1) res = 2;
            else if (w[1] == 2) res = 1;
            out.println(res);
            out.flush();
            return;
        }

        for (int i = 0; i < M; i++) cnt[i] = cnt(i);
        // 初始化第一个数
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (cnt[i] + cnt[j] == w[1])
                    f[1][i][j] = 1;
            }
        }

        // 中间的数
        for (int i = 2; i < n; i++) {
            for (int j = 0; j < M; j++) {
                for (int k = 0; k < M; k++) {
                    f[i & 1][j][k] = 0;
                }
            }
            for (int j = 0; j < M; j++) {
                for (int k = 0; k < M; k++) {
                    if (cnt[j] + cnt[k] == w[i]) {
                        for (int a = 0; a < 2; a++) {
                            for (int b = 0; b < 2; b++) {
                                f[i & 1][j][k] = (f[i & 1][j][k] + f[i - 1 & 1][(j >> 1) | (a << 2)][(k >> 1) | (b << 2)]) % P;
                            }
                        }
                    }
                }
            }
        }

        long res = 0;
        for (int j = 0; j < M; j++) {
            for (int k = 0; k < M; k++) {
                f[n & 1][j][k] = 0;
            }
        }
        // 最后一个数
        for (int i = 0; i < 4; i++) {
            // 状态只能是 xx0 x ∈ {0, 1}
            int x = i << 1;
            for (int j = 0; j < 4; j++) {
                int y = j << 1;
                if (cnt[x] + cnt[y] == w[n]) {
                    for (int a = 0; a < 2; a++) {
                        for (int b = 0; b < 2; b++) {
                            f[n & 1][x][y] = (f[n & 1][x][y] + f[n - 1 & 1][(x >> 1) | (a << 2)][(y >> 1) | (b << 2)]) % P;
                        }
                    }
                }
                res = (res + f[n & 1][x][y]) % P;
            }
        }

        out.println(res);
        out.flush();
    }

    static int cnt(int x) {
        int res = 0;
        for (int i = 0; i < 3; i++) {
            res += x >> i & 1;
        }
        return res;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

